/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		err.cpp
 * 位 置:
 *		src/pub/err.cpp
 * 描 述:
 *		公用异常类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-14  Simon   - 创建
 *
 **************************************************************/

extern "C"
{
#include <string.h>
}
#include "pub/def.h"
#include "pub/err.h"

using namespace std;

//----------------------------------------------
//	缺省构造函数
//----------------------------------------------
AppException::AppException() throw()
{
	AppException(ERRCODE_APP, "应用程序错误");
}

//----------------------------------------------
//	缺省构造函数
//----------------------------------------------
AppException::AppException(const char* msg) throw()
{
	AppException(ERRCODE_APP, msg);
}

//----------------------------------------------
//	构造函数
//----------------------------------------------
AppException::AppException(unsigned code, const char* msg) throw()
{
	setCode(code);
	setMsg(msg);
}

//----------------------------------------------
//	析构函数
//----------------------------------------------
AppException::~AppException() throw()
{
}

//----------------------------------------------
//	返回错误代码
//----------------------------------------------
unsigned AppException::getCode() const
{
	return code;
}

//----------------------------------------------
//	返回错误信息
//----------------------------------------------
const char* AppException::getMsg() const
{
	return msg;
}

//----------------------------------------------
//	设置错误代码
//----------------------------------------------
AppException* AppException::setCode(unsigned code)
{
	this->code = code;
	return this;
}

//----------------------------------------------
//	设置错误代码
//----------------------------------------------
AppException* AppException::setMsg(const char* msg)
{
	unsigned len = (sizeof(this->msg) > strlen(msg)) ? strlen(msg) : sizeof(this->msg);
	memset(this->msg, 0x00, sizeof(this->msg));
	memcpy(this->msg, msg, len);
	return this;
}

//----------------------------------------------
//	设置错误代码和信息
//----------------------------------------------
AppException* AppException::set(unsigned code, const char* msg)
{
	setCode(code);
	setMsg(msg);
	return this;
}
